﻿// Learn more about F# at http://fsharp.net
module Mailbox

open System.Runtime.InteropServices
open System
open System.Collections.Generic

type Agent<'T> = MailboxProcessor<'T>

let agentSample1() = 
    let agent =
       Agent.Start(fun inbox ->
         async { 
            let strings = dict Seq.empty
            for i in [1..5] do 
                let! msg = inbox.Receive()
                if strings.ContainsKey msg then 
                    strings.[msg] <- strings.[msg] + 1
                else 
                    strings.[msg] <- 0
                printfn "message '%s' now seen '%d' times" msg strings.[msg] 
        } )
    agent.Error.Add(fun e-> printfn "agent ocurror error ,msg:%s" e.Message)
    printfn "because the message is running on the background thread, please see the result at the console window behind"
    agent.Post("msg1");
    agent.Post("msg2");
    agent.Post("msg3");
    agent.Post("msg1");
    agent.Post("msg2");

agentSample1()


Console.ReadKey() |> ignore